package cn.flying.cloud.flow.core.listener.event.callback;

import javax.annotation.Resource;

import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.flying.cloud.flow.bo.FwProcInstBo;
import cn.flying.cloud.flow.service.FwProcInstService;

/**
 * 任务分配监听
 *
 * @author: admin
 * @date: 2023年02月26日 08:24
 * @version: 1.0
 */
@Component
public class TaskAssignedListenerCallback implements EventListenerCallback {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private FwProcInstService procInstService;

    @Override
    public void execute(FlowableEvent event) {
        logger.info("==========任务分配监听器回调处理---start===========");
        logger.info("监听事件类型：【{}】", event.getType().name());
        FlowableEntityEventImpl entityEvent = (FlowableEntityEventImpl) event;
        TaskEntityImpl entity = (TaskEntityImpl) entityEvent.getEntity();

        FwProcInstBo inst = new FwProcInstBo();
        inst.setProcInstId(entity.getProcessInstanceId());
        inst = procInstService.getOneObj(inst);
        inst.setTaskId(entity.getId());
        inst.setAssignee(entity.getAssignee());
        procInstService.updateObj(inst);

        logger.info("任务【{}-{}】分配代理人【{}】，当前执行ID：【{}】", entity.getName(), entity.getId(), entity.getAssignee(), entityEvent.getExecutionId());
        logger.info("==========任务分配监听器回调处理---end===========");
    }
}
